/*
 * Copyright (C) 2004, Velocityme Partnership
 * ShowAccessAction.java
 *
 * Created on 29 August 2004, 12:03
 */

package com.velocityme.www.interfaces;

import com.velocityme.entity.Group;
import com.velocityme.entity.GroupAccess;
import com.velocityme.entity.Node;
import com.velocityme.entity.User;
import com.velocityme.entity.UserAccess;
import com.velocityme.enums.Permission;
import com.velocityme.session.KeyLocal;
import com.velocityme.session.NodeLocal;
import com.velocityme.session.SearchLocal;
import com.velocityme.www.actionforms.NodeIdActionForm;
import com.velocityme.www.dto.AccessDTO;
import java.util.ArrayList;
import java.util.Collection;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author  Robert
 */
public class ShowAccessAction extends org.apache.struts.action.Action {
    
    /** Creates a new instance of ShowAccessAction */
    public ShowAccessAction() {
    }
    
    @Override
    public ActionForward execute(ActionMapping mapping, 
        ActionForm form,  HttpServletRequest request, HttpServletResponse response) throws Exception {
        try{
            NodeIdActionForm nodeAF = (NodeIdActionForm)form;
            HttpSession session = request.getSession();
            KeyLocal key = (KeyLocal)session.getAttribute("sessionKey");
            Long nodeId = nodeAF.getNodeId();
            NodeLocal nodeLocal = lookupNodeBean();
            Node node = nodeLocal.findNode(key, nodeId);
            SearchLocal searchLocal = lookupSearchBean();
            Collection<AccessDTO> accessDTOs = new ArrayList<AccessDTO>();
            if (node instanceof Group) {
                Group group = (Group)node;
                session.setAttribute("group", group);
                for (GroupAccess groupAccess: group.getGroupAccessesGroup()) {
                    Boolean isDeletable = Boolean.valueOf(searchLocal.getAssignedPermissions(key, groupAccess.getNode()).contains(Permission.GROUP_ACCESS_DELETE));
		    Long groupAccessId = groupAccess.getId();
                    String groupName = groupAccess.getGroup().getName();
                    accessDTOs.add(new AccessDTO(isDeletable, groupAccessId, groupAccess.getNode(), groupName, groupAccess.getRole()));
                }
                session.setAttribute("groupAccessByGroup", accessDTOs);
                ShowNodeAction.setupTreeDisplay(session, node, "My Access");
                return mapping.findForward("ACTION_SHOW_GROUP_SUCCESS");
            }
            if (node instanceof User) {
                User user = (User)node;
                session.setAttribute("user", user);
                for (UserAccess userAccess: user.getUserAccessesUser()) {
                    Boolean isDeletable = Boolean.valueOf(searchLocal.getAssignedPermissions(key, userAccess.getNode()).contains(Permission.USER_ACCESS_DELETE));
		    Long userAccessId = userAccess.getId();
                    String userName = userAccess.getUser().getName();
                    accessDTOs.add(new AccessDTO(isDeletable, userAccessId, userAccess.getNode(), userName, userAccess.getRole()));
                }
                session.setAttribute("userAccessByUser", accessDTOs);
                ShowNodeAction.setupTreeDisplay(session, node, "My Access");
                return mapping.findForward("ACTION_SHOW_USER_SUCCESS");
            }
        }
        catch(Exception e){
            request.setAttribute("Additional_Info", e);
        }
        return mapping.findForward("ACTION_SHOW_ACCESS_FAILED");
    }

    private NodeLocal lookupNodeBean() {
        try {
            Context c = new InitialContext();
            return (NodeLocal) c.lookup("java:comp/env/NodeBean");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }

    private SearchLocal lookupSearchBean() {
        try {
            Context c = new InitialContext();
            return (SearchLocal) c.lookup("java:comp/env/SearchBean");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }
    
}
